//
//  ViewController.m
//  CAAnimation动画
//
//  Created by zxy on 2018/4/26.
//  Copyright © 2018年 zssh. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *aniVIew;

@property (weak, nonatomic) IBOutlet UIImageView *aniImgV;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self caBaseAnimation];
    //[self caKeyframeAnimation];

}

#pragma mark -CABaseAnimation
-(void)caBaseAnimation{
    
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"position"];
    /*动画结束时是否移除动画，需配合fillModel*/
    ani.removedOnCompletion = NO;
    /*保存动画结束时的状态*/
    ani.fillMode = kCAFillModeForwards;
    ani.duration = 2;
    ani.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
    [self.aniVIew.layer addAnimation:ani forKey:nil];
}

#pragma mark -CAKeyframeAnimation
/*
    CAKeyframeAnimation也是根据属性展示动画的，他与CABasicAnimation的不同点在于他可以指定多个状态，不局限于始末状态，这样你的动画将更加灵活
 */
-(void)caKeyframeAnimation{
    
    NSArray *values = @[[NSValue valueWithCGPoint:CGPointMake(200, 200)],[NSValue valueWithCGPoint:CGPointMake(300, 300)],[NSValue valueWithCGPoint:CGPointMake(100, 300)]];
    
    CAKeyframeAnimation *ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ani.values = values;
    ani.fillMode = kCAFillModeForwards;
    ani.removedOnCompletion = NO;
    ani.duration = 3;
    ani.repeatCount = MAXFLOAT;
    
    /*我改变了calculationMode这个属性，有没有发现转折点出动作平滑了许多？恩，这个属性是用来改变补间动画的计算模式的。这个属性是CAKeyframeAnimation独有的属性*/
    ani.calculationMode = kCAAnimationCubicPaced;
    
    //keyTimes属性指定的是当前状态节点到初始状态节点的时间占动画总时长的比例。若果不设置keyTimes则匀速播放
//    ani.keyTimes = @[@0,@0.8,@1];
    ani.keyTimes = @[@0,@0.25,@0.5,@0.75,@1];
    
    //bezierPathWithOvalInRect: 根据一个Rect画一个椭圆曲线
    //指定了path属性，所以这时values属性将被忽略
    ani.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 200, 200, 200)].CGPath;
    
    /*kCAAnimationRotateAuto或者* kCAAnimationRotateAutoReverse*。如果这个属性设置成以上两个值中的任意一个，当前layer都会始终保持朝向运动方向，也就是跟随运动方向自动旋转*/
    ani.rotationMode = kCAAnimationRotateAuto;
    
    [self.aniVIew.layer addAnimation:ani forKey:@"keyframe"];
    

}

#pragma mark -CATransition动画-不知道为什么需要将代码放到点击事件里才有用

- (IBAction)transition:(UIButton *)sender {
    CATransition *ani = [CATransition animation];
    ani.duration = 3;
    ani.fillMode = kCAFillModeForwards;
    /**cube:立方体效果 suckEffect:收缩效果，如一块布被抽走 oglFlip:上下翻转效果,
     rippleEffect:滴水效果 pageCurl:向上翻一页  pageUnCurl:向下翻一页,
     cameralIrisHollowOpen:摄像头打开效果  cameraIrisHollowClose:摄像头关闭效果*/
    ani.type = @"cameraIrisHollowClose";
    ani.subtype = kCATransitionFromTop;
    
    [self.aniImgV.layer addAnimation:ani forKey:nil];
    
    self.aniImgV.image = [UIImage imageNamed:@"hotel_bg.jpg"];
}

#pragma mark -CATransition
-(void)caTransiton{
    
    
    CATransition *ani = [CATransition animation];
    ani.duration = 5;
    ani.fillMode = kCAFillModeForwards;
    /**cube:立方体效果 suckEffect:收缩效果，如一块布被抽走 oglFlip:上下翻转效果,
     rippleEffect:滴水效果 pageCurl:向上翻一页  pageUnCurl:向下翻一页,
     cameralIrisHollowOpen:摄像头打开效果  cameraIrisHollowClose:摄像头关闭效果 cameraIrisHollowClose*/
    ani.type = @"rippleEffect";
    ani.subtype = kCATransitionFromTop;
    
    [self.aniImgV.layer addAnimation:ani forKey:nil];
    
    self.aniImgV.image = [UIImage imageNamed:@"hotel_bg.jpg"];

}
@end
